#include "calibrationwidget.h"
#include "ui_calibrationwidget.h"
#include <QMessageBox>
#include "serialportset.h"
calibrationWidget::calibrationWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::calibrationWidget)
{
    ui->setupUi(this);
    stateProgressBar = new QtStateProgressBar(this);
    stateProgressBar->setType(QtStateProgressBar::ACK); // NACK, ACK
    QStringList states;
    states<<tr("第一步")<<tr("第二步")<<tr("第三步")<<tr("第四步")<<tr("第五步")<<tr("第六步")<<tr("完成");
    stateProgressBar->setStates(states);
    stateProgressBar->setMaximum(7);
    stateProgressBar->setTip("已完成");
    stateProgressBar->setValue(0);

    ui->verticalLayout->addWidget(stateProgressBar);
    labelYesHide();

//    connect(serialPortSet::Instance(), SIGNAL(caliRead()), this, SLOT(handleSerialReturn(QString)));
    connect(serialPortSet::Instance(), SIGNAL(initCaliFailed()), this, SLOT(setInitFailed()));
}

calibrationWidget::~calibrationWidget()
{
    delete ui;
}
//第二步
void calibrationWidget::on_checkBox_clicked(bool checked)
{
    if(checked)
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "吸阻单元是否确认已拉出？",QMessageBox::Yes|QMessageBox::No);
        switch (result)
        {
        case QMessageBox::No:
            ui->checkBox->setChecked(false);
            return;
        case QMessageBox::Yes:
            ui->lineEdit->setDisabled(false);
            stateProgressBar->setValue(2);
            ui->label_12->show();
            ui->checkBox->setDisabled(true);
            ui->checkBox_2->setDisabled(false);
            break;
        default:
            return;
        }
//        stateProgressBar->setValue(2);
//        ui->label_12->show();
//        ui->checkBox_2->setDisabled(false);
    }
//    if(!checked)
//    {
//        stateProgressBar->setValue(1);
//        ui->label_12->hide();
//        ui->checkBox_2->setDisabled(true);
//    }
//    if(!ui->checkBox_2->isChecked() && !checked)   //第三步未勾选
//    {
//        stateProgressBar->setValue(1);
//        ui->label_12->hide();
//    }
//    if(ui->checkBox_2->isChecked() && !checked)
//    {
//        ui->checkBox->setChecked(true);
//        return;
//    }
}
//第三步
void calibrationWidget::on_checkBox_2_clicked(bool checked)
{
    if(!isCheck)//校准
    {
        if(checked)
        {
            if(ui->lineEdit->text().toInt() <= 0)
            {
                QMessageBox::warning(NULL, "警告", "请核对标准值！");
                ui->checkBox_2->setChecked(false);
                return;
            }
            emit sendStandardValue(ui->lineEdit->text());
            QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "请确认标准棒是否已放入并且已输入标准值。",QMessageBox::Yes|QMessageBox::No);
            switch (result)
            {
            case QMessageBox::No:
                ui->checkBox_2->setChecked(false);
                return;
            case QMessageBox::Yes:
                ui->lineEdit->setDisabled(true);
                stateProgressBar->setValue(3);
                ui->label_13->show();
                ui->checkBox_2->setDisabled(true);
                serialPortSet::Instance()->sendStandardValue2Com();
                break;
            default:
                return;
            }
        }
    }
    else//验证
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "请确认标准棒是否已放入。",QMessageBox::Yes|QMessageBox::No);
        switch (result)
        {
        case QMessageBox::No:
            ui->checkBox_2->setChecked(false);
            return;
        case QMessageBox::Yes:
            ui->lineEdit->setDisabled(true);
            stateProgressBar->setValue(3);
            ui->label_13->show();
            ui->checkBox_2->setDisabled(true);
//            serialPortSet::Instance()->sendStandardValue2Com();
            serialPortSet::Instance()->sendCaliCheck2Com();
            break;
        default:
            return;
        }
    }

}

void calibrationWidget::labelYesHide()
{
    ui->label_11->hide();
    ui->label_12->hide();
    ui->label_13->hide();
    ui->label_14->hide();
    ui->label_15->hide();
    ui->label_16->hide();

    ui->checkBox->setDisabled(true);
    ui->checkBox_2->setDisabled(true);
    ui->checkBox_3->setDisabled(true);
    ui->checkBox_4->setDisabled(true);

}
//开始校准
void calibrationWidget::on_pushButton_clicked()
{
//    emit ui->pushButton_2->clicked();
    if(!isInit)
    {
        QMessageBox::warning(NULL, "提示", "初始化失败，请检查通信接口并重启程序或联系管理员!");
        return;
    }
    {   //取消校准操作（复位操作）
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
        ui->checkBox->setChecked(false);
        ui->checkBox_2->setChecked(false);
        ui->checkBox_3->setChecked(false);
        ui->checkBox_4->setChecked(false);
        labelYesHide();
        stateProgressBar->setValue(0);
        serialPortSet::Instance()->sendCaliCancle2Com();
    }
    if(!isCheck)//校准
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "是否开始校准？",QMessageBox::Yes|QMessageBox::No);

        switch (result)
        {
        case QMessageBox::No:
            return;
        case QMessageBox::Yes:
            break;
        default:
            return;
        }
    }else
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "是否开始验证？",QMessageBox::Yes|QMessageBox::No);

        switch (result)
        {
        case QMessageBox::No:
            return;
        case QMessageBox::Yes:
            break;
        default:
            return;
        }
    }
    startCalibration();//开始校准
}
void calibrationWidget::startCalibration()
{
    emit startSignal();
    serialPortSet::Instance()->sendCaliZero2Com();
    //delete
}
//取消校准
void calibrationWidget::on_pushButton_2_clicked()
{
    if(!isCheck)//校准
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "是否取消校准？",QMessageBox::Yes|QMessageBox::No);
        switch (result)
        {
        case QMessageBox::No:
            return;
        case QMessageBox::Yes:
            break;
        default:
            return;
        }
    }
    else
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "是否取消验证？",QMessageBox::Yes|QMessageBox::No);
        switch (result)
        {
        case QMessageBox::No:
            return;
        case QMessageBox::Yes:
            break;
        default:
            return;
        }
    }
    emit stopSignal();
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
    ui->checkBox->setChecked(false);
    ui->checkBox_2->setChecked(false);
    ui->checkBox_3->setChecked(false);
    ui->checkBox_4->setChecked(false);
    labelYesHide();
    stateProgressBar->setValue(0);
    serialPortSet::Instance()->sendCaliCancle2Com();
}

void calibrationWidget::handleSerialReturn(QString bufstr)
{
    if(!bufstr.contains("030d0a"))
    {
        return;
    }
    tmpStr = "";
    if(!bufstr.contains("3035"))//不是校准返回的指令
    {
        return;
    }
    QString s = bufstr.mid(14, 4);
    if(s.toInt() == 3131) //吸阻零点回复
    {
        firstComplete2Second();
    }
    if(s.toInt() == 3132)
    {
        QString value = bufstr.mid(18, 4);
//        qDebug()<<"吸阻：：："<<bufstr;
        thirdComplete2fourth(value);
    }
    if(s.toInt() == 3137)
    {
        QString value = bufstr.mid(18, 4);
        qDebug()<<"验证："<<bufstr;
        newThird2fourth(value);
    }
}
//第三步完成到第四步
void calibrationWidget::thirdComplete2fourth(QString str)
{
    int value = str.toInt(0, 16);
    ui->lineEdit_2->setText(QString::number(value));
    ui->label_14->show();
    stateProgressBar->setValue(4);
    ui->checkBox_3->setDisabled(false);
}
void calibrationWidget::newThird2fourth(QString str)
{
    int value = str.toInt(0, 16);
    ui->lineEdit_2->setText(QString::number(value));
    ui->label_14->show();
    stateProgressBar->setValue(4);
    ui->checkBox_3->setDisabled(false);
}

void calibrationWidget::firstComplete2Second()
{
    ui->label_11->show();//第一步  √  显示
    ui->checkBox->setDisabled(false);   //第二步checkbox可编辑
    stateProgressBar->setValue(1);
}
//第五步
void calibrationWidget::on_checkBox_3_clicked(bool checked)
{
    if(checked)
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "请确认是否已移除标准棒。",QMessageBox::Yes|QMessageBox::No);
        switch (result)
        {
        case QMessageBox::No:
            ui->checkBox_3->setChecked(false);
            return;
        case QMessageBox::Yes:
            ui->label_15->show();
            ui->checkBox_3->setDisabled(true);
            stateProgressBar->setValue(5);
            ui->checkBox_4->setDisabled(false);
            break;
        default:
            return;
        }
    }

}

void calibrationWidget::on_checkBox_4_clicked(bool checked)
{
    if(checked)
    {
        QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "请确认是否已将吸阻单元推入“测量”位置。",QMessageBox::Yes|QMessageBox::No);
        switch (result)
        {
        case QMessageBox::No:
            ui->checkBox_4->setChecked(false);
            return;
        case QMessageBox::Yes:
            serialPortSet::Instance()->sendCaliSuccess2Com();
            ui->label_16->show();
            ui->checkBox_4->setDisabled(true);
            stateProgressBar->setValue(7);
            QMessageBox::information(NULL, "提示", "已完成！");
            break;
        default:
            return;
        }
    }
}


void calibrationWidget::on_checkBox_5_clicked(bool checked)
{
    if(checked)
    {
        isCheck = true;
        ui->label_10->setText("*点击开始验证功能。");
        ui->label_7->setText("*点击取消将中止验证功能。");
        ui->label_3->setText("3.放入标准棒。");
        ui->lineEdit->hide();
        ui->label_4->setText("4.验证中。验证结果");
    }
    else
    {
        isCheck = false;
        ui->label_10->setText("*点击开始校准功能。");
        ui->label_7->setText("*点击取消将中止校准功能。");
        ui->label_3->setText("3.放入标准棒并输入标准值。");
        ui->lineEdit->show();
        ui->label_4->setText("4.验证中。验证结果");
    }
}

void calibrationWidget::setInitFailed()
{
    isInit = false;
}
